<?php

class Cache
{
 	function setFile($sName, $sVariable = "")
 	{
 	 	$this->sName = $sName;
 	 	$this->sVariable = ($sVariable ? $sVariable : $this->getVarName($sName));
 	}

 	function getVarName($sName)
 	{
		$aGetName = explode(".", $sName);
		if ( isset($aGetName[0]) )
		{
			return $aGetName[0];
		}
 	}

 	function doRequire($sVarName = "")
 	{
 	    if ( defined("benchMark") AND benchMark AND App::getParam('debug') )
		{
			$obenchMark = new BenchMark;
			$obenchMark->start();
		}

		if ( !file_exists(cachedDir . $this->fName($this->sName)) )
		{
			trigger_error("Cant load the cache file: ". $this->sName .". To fix this please refresh your browser.", E_USER_ERROR);
		}

 	 	require cachedDir . $this->fName($this->sName);

		if ( defined("benchMark") AND benchMark AND App::getParam('debug') )
		{
			global $aCacheIncluded;
			$aCacheIncluded[] = array
			(
				'name' => cachedDir . $this->sName,
				'time' => $obenchMark->finish2(),
			);
			unset($obenchMark);
		}

 	 	$sVariableName = $this->sVariable;

		unset($this->sName);
		unset($this->sVariable);

		if ( $sVarName )
		{
			return $$sVarName;
		}

 	 	if ( isset($this->$sVariableName) )
 	 	{
	 		return $this->$sVariableName;
 	 	}
 	}

 	function isRequire($sName)
 	{
 	    if ( defined("benchMark") AND benchMark AND App::getParam('debug') )
		{
			$obenchMark = new BenchMark;
			$obenchMark->start();
		}

		if ( !file_exists(cachedDir . Cache::fName($sName)) )
		{
			trigger_error("Cant load the cache file: ". $sName .". To fix this please refresh your browser.", E_USER_ERROR);
		}

		require cachedDir . Cache::fName($sName);

		if ( defined("benchMark") AND benchMark AND App::getParam('debug') )
		{
			global $aCacheIncluded;
			$aCacheIncluded[] = array
			(
				'name' => cachedDir . $sName,
				'time' => $obenchMark->finish2(),
			);
			unset($obenchMark);
		}

 	 	$sVariableName = Cache::getVarName($sName);
		return $this->$sVariableName;
 	}

 	function fName($sName)
 	{
 		if ( !cacheEncode )
 		{
 			return $sName;
 		}

 		$sName = str_replace(".php", "", $sName);
 		$sName = base64_encode($sName . cacheSecret) . ".php";
 		return $sName;
 	}

	function isFile($sName)
    {
    	if ( file_exists(cachedDir . Cache::fName($sName)) )
    	{
    		return true;
    	}
    	return false;
    }

	function isCached($iCheckTime = "")
    {
    	if ( file_exists(cachedDir . $this->fName($this->sName)) )
    	{
    		if ( !empty($iCheckTime) AND (time() - $iCheckTime * 60) > (filemtime(cachedDir . $this->fName($this->sName))) )
    		{
    			return false;
    		}
    		return true;
    	}
    	return false;
    }

	function save($mContent, $bAddThis = true)
	{
		$content = "<?php\n ";
		$content .= ''. ($bAddThis ? '$this->' : '') .''. $this->sVariable .'= array(' . "\n";

		if ( !is_array($mContent) )
		{
			$content .= $mContent;
		}
		else
		{

			$sub_content = '';
			foreach($mContent as $m => $v)
			{
				if ( is_array($v) )
				{
					$bIsArray = true;
					break;
				}
				$sub_content .= "'". $m ."' => '". str_replace("'", "\'", $v) ."',";
			}

			if ( !isset($bIsArray) )
			{
				$content .= $sub_content;
			}
			else
			{
				foreach($mContent as $m)
				{
					$content .= 'array(';
					foreach($m as $a => $v)
					{
						 $content .= "'". $a ."' => '". str_replace("'", "\'", $v) ."',";
					}
					$content .= '),';
				}
			}
		}

		$content .= "); \n ?>";
		$this->createCached($content);
	}

	function subStrRemove($sName)
	{
		if ( is_writable(cachedDir) && is_dir(cachedDir) )
		{
			$hDir = opendir(cachedDir);
			while($sFile = readdir($hDir))
			{
				if ( substr($sFile, 0, strlen($sName)) == $sName )
				{
					@unlink(cachedDir . Cache::fName($sFile));
				}
			}
		}
	}

    function createCached($sContent)
    {
   		$handle = fopen(cachedDir . $this->fName($this->sName), 'w+');
   		fwrite($handle, $sContent);
   		fclose($handle);
    }

    function cleanFile($sStr)
    {
     	$sStr = str_replace('"','\"',$sStr);
     	return $sStr;
    }

    function rmFile($sName)
    {
    	if ( file_exists(cachedDir . Cache::fName($sName)) )
    	{
     		@unlink(cachedDir . Cache::fName($sName));
     	}
    }

    function clearTemplates()
    {
		App::loadClass('file/File');
   		CFile::deleteRecursiveFolder(App::getParam('template', 'p_dir'));
    }
}

?>